<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\admin\model\AdminMenu;
use app\admin\model\Admin;
use app\common\model\CustomerPrice;
use Firebase\JWT\JWT;
use app\common\model\Customer;
use app\common\model\CustomerHetong;
use think\facade\Log;

class Index extends AdminBase
{
    protected $noLogin = ['index','login','ssss'];
    protected $noAuth = ['get_menu','get_admin_user'];

    /**
     * 跳转默认首页
     */
    public function index()
    {
        $this->redirect('/admin');
    }

    /**
     * 首页数据
     */
    public function home()
    {
        $data['hetong'] = CustomerHetong::whereBetweenTime('end_time',date('Y-m-d'),date('Y-m-d',strtotime(date('Y-m-d') . '+30 day')))->where('is_renew',0)->count();
        $data['hetong_new'] = CustomerHetong::whereMonth('create_time')->count();
        $data['total_price'] = round(CustomerHetong::sum('price'),2);
        $data['total_price_ok'] = round(CustomerPrice::where('hetong_id','>',0)->sum('price'),2);
        $data['total_price_no'] = round($data['total_price']-$data['total_price_ok'],2);
        $data['price_month'] = round(CustomerPrice::whereMonth('datetime')->sum('price'),2);

        $this->result(compact('data'));
    }

    /**
     * 后台登陆
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function login()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //检查验证码情况
            $user = Admin::where(['username'=>$param['username'],'password'=>md5($param['password'])])->find();
            if(empty($user)){
                $this->result([],1, '账号或密码错误!','json');
            }
            // 保存用户信息
            $userinfo = [
                'user_id'  => $user['id'],
                'name'     => $user['name'],
                'username' => $user['email'],
                'type'     => 'admin',
                'exp'      => time()+86400,
                'iat'      => time(),
            ];
            // 生成token
            $access_token = JWT::encode($userinfo,config('jwt')['key']);
            Admin::where('id',$user['id'])->inc('login_count')->update(['last_login_time'=>time(),'last_login_ip'=>request()->ip()]);
            $this->result(compact('access_token'),0, '登陆成功','json');
        }
    }

    /**
     * 左侧菜单
     */
    public function get_menu()
    {
        $code = 0;
        $data = AdminMenu::getMenu();
        if($data == false){
            $code = 401;
            return compact('code');
        }
        return compact('code','data');
    }

    /**
     * 读取管理员信息
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get_admin_user()
    {
        // 读取用户信息
        $data = Admin::field('mobile,name as nickname,avatarurl as avatar,roles')->find(AdminId());
        $data['authorities'] = AdminMenu::getMenu();
        $this->result($data);
    }



}
